﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baza.EF.DAL;
using Baza.EF.BLL;
using System.Web.Security;


namespace Pep_3014_3070.Anonymous
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void SingInButton_Click(object sender, EventArgs e)
        {

            if (EmailInput.Text != "" && PasswordInput.Text != "")
            {

                int tipKorisnika = DAAdmin.getTipusera(EmailInput.Text);
                //1 = Owner , 2 = Customer, 3 = User 
                if (tipKorisnika == 1)
                {
                    ad_Login_Owner_By_Email_Result o = DAAdmin.getOwnersByEmail(EmailInput.Text, PasswordInput.Text);

                    //ako je null vrijednost to znaci da su netocni korisnicki podaci, ili da ne postoji korisnik. . . 
                    if (o != null)
                    {
                        Session.Add("UserName", o.FirstName + " " + o.LastName);
                        Session.Add("TipKorisnika", o.TypeID);
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(o.OwnerID.ToString(), false, 480);
                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        HttpCookie loginCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        Response.Cookies.Add(loginCookie);

                        if (Request.QueryString["ReturnUrl"] != null)
                            Response.Redirect(FormsAuthentication.GetRedirectUrl(o.OwnerID.ToString(), false));
                        else
                            Response.Redirect("/OwnersHome");
                    }
                    else
                    {
                        //Snimiti podatke tko pokusava da se logira . . .
                        SnimiPokusaj();
                        EmailInput.Text = "";
                        EmailInput.Focus();
                    }
                }
                else if (tipKorisnika == 2)
                {
                    ad_Login_Customer_By_Email_Result o = DAAdmin.getCustomersByEmail(EmailInput.Text, PasswordInput.Text);
                    //ako je null vrijednost to znaci da su netocni korisnicki podaci, ili da ne postoji korisnik. . . 
                    if (o != null)
                    {
                        Session.Add("UserName", o.FirstName + " " + o.LastName);
                        Session.Add("TipKorisnika", o.TypeID);
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(o.CustomerID.ToString(), false, 480);
                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        HttpCookie loginCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        Response.Cookies.Add(loginCookie);

                        if (Request.QueryString["ReturnUrl"] != null)
                            Response.Redirect(FormsAuthentication.GetRedirectUrl(o.CustomerID.ToString(), false));
                        else
                            Response.Redirect("/CustomersHome");

                    }
                    else
                    {
                        //Snimiti podatke tko pokusava da se logira . . .
                        SnimiPokusaj();
                    }

                }
                else if (tipKorisnika == 3)
                {
                    ad_Login_User_By_Email_Result o = DAAdmin.getUsersByEmail(EmailInput.Text, PasswordInput.Text);
                    //ako je null vrijednost to znaci da su netocni korisnicki podaci, ili da ne postoji korisnik. . . 
                    if (o != null)
                    {
                        //Trebalo bi provjeravati uloge/role korisnika na sistemu , pa na osnovu toga prosljediti korisnika na njegov dio!
                        Session.Add("UserName", o.FirstName + " " + o.LastName);
                        Session.Add("TipKorisnika", o.TypeID);
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(o.UserID.ToString(), false, 480);
                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        HttpCookie loginCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        Response.Cookies.Add(loginCookie);

                        if (Request.QueryString["ReturnUrl"] != null)
                            Response.Redirect(FormsAuthentication.GetRedirectUrl(o.UserID.ToString(), false));
                        else
                            Response.Redirect("/UsersHome");

                    }
                    else
                    {
                        //Snimiti podatke tko pokusava da se logira . . .
                        SnimiPokusaj();
                    }
                }
                else
                {
                    SnimiPokusaj();
                    EmailInput.Text = "";
                    EmailInput.Focus();
                }
            }
        }

        private void SnimiPokusaj()
        {
            //Logiranje l = new Logiranje();
            //l.Email = username.Text;
            //l.IpAdresa = Request.UserAgent;
            //l.MacAdresa = "<Nepoznato>";
            //l.WebBrowser = Request.Browser.Id;
            //l.ImeRacunara = "<Nepoznato>";
            //DAAdmin.TryLogin(l);
        }
    }
}